package com.sun.controller;

import com.github.pagehelper.PageInfo;
import com.sun.enums.HttpEnum;
import com.sun.pojo.entity.PurchaseOrders;
import com.sun.pojo.entity.PurchaseSettlement;
import com.sun.pojo.vo.R;
import com.sun.service.PurchaseOrdersService;
import com.sun.service.PurchaseSettlementService;
import com.sun.utils.DingDingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 采购订单表 记录采购订单信息(PurchaseOrders)表控制层
 *
 * @author makejava
 * @since 2024-10-25 08:18:16
 */
@RestController
@RequestMapping("purchaseOrders")
@CrossOrigin
public class PurchaseOrdersController {
    /**
     * 服务对象
     */
    @Autowired
    PurchaseOrdersService purchaseOrdersService;
    @Autowired
    PurchaseSettlementService purchaseSettlementService;

    /**
     * 分页查询
     *
     * @param purchaseOrders 筛选条件
     * @param pageNum        分页对象
     * @return 查询结果
     */
    @GetMapping
    public R queryByPage(PurchaseOrders purchaseOrders, Integer pageNum) {
        PageInfo pageInfo = purchaseOrdersService.queryByPage(purchaseOrders, pageNum);
        return R.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") String id) {
        return R.success(purchaseOrdersService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param purchaseOrders 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody PurchaseOrders purchaseOrders) {
        return R.success(purchaseOrdersService.insert(purchaseOrders));
    }

    /**
     * 编辑数据
     *
     * @param purchaseOrders 实体
     * @return 编辑结果
     */
    @PutMapping
    public R edit(@RequestBody PurchaseOrders purchaseOrders) {
        return R.success(purchaseOrdersService.update(purchaseOrders));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") String id) {
        return R.success(purchaseOrdersService.deleteById(id));
    }

    /**
     * 发货
     *
     * @param orderId 主键
     * @return 发货情况
     */
    @GetMapping("/deliver")
    public R deliver(String orderId, String updatedBy) {
        // 把订单中的数据拿出并且加入到结算表
        PurchaseSettlement purchaseSettlement = new PurchaseSettlement();
        // 存入ID
        purchaseSettlement.setPoId(orderId);
        // 通过id查询表单数据获取总价格
        PurchaseOrders purchaseOrders = purchaseOrdersService.queryById(orderId);
        purchaseSettlement.setTotalAmount(purchaseOrders.getTotalAmount());
        purchaseSettlement.setSettlementStatus("0");
        purchaseSettlement.setCreatedBy(updatedBy);
        if (purchaseSettlementService.insert(purchaseSettlement)>0) {
            // 更新订单状态
            return purchaseOrdersService.deliver(orderId, updatedBy);
        }
        return R.fail(HttpEnum.WARN.getCode(),"发货失败");
    }


    /**
     * 总交易额
     *
     * @return 交易额
     */
    @GetMapping("/totalAmount")
    public R totalAmount(String updatedAt) {
        return R.success(purchaseOrdersService.totalAmount(updatedAt));
    }


    /**
     * 周总交易额
     *
     * @return 交易额
     */
    @GetMapping("/totalAmountWeek")
    public R totalAmountWeek() {
        return R.success(purchaseOrdersService.totalAmountWeek());
    }


    /**
     * 年的月交易额
     *
     * @return 交易额
     */

    @GetMapping("/totalAmountMonth")
    public R totalAmountMonth(){
        return R.success(purchaseOrdersService.totalAmountMonth());
    }

     /**
     * 待收货易额
     *
     * @return 交易额
     */
    @GetMapping("/queryWaitingAmount")
    public R queryWaitingAmount(){
        return R.success(purchaseOrdersService.queryWaitingAmount());
    }
}

